//
//  PlanDetailController.m
//  Water
//
//  Created by yh on 2017/11/27.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "PlanDetailController.h"
#import "PlanDatePicker.h"
#import "PlanItem.h"
#import "PlanTagController.h"
#import "RingsController.h"
#import <BlocksKit/BlocksKit+UIKit.h>
@interface PlanDetailController ()

@end

@implementation PlanDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
    
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    self.viewModel.plan = self.plan;
    [self.viewModel.refresh subscribeNext:^(id  _Nullable x) {
        [self.tableView reloadData];
    }];
    [self.viewModel setDefultMenus];
    
}
- (void)setupViews{
    PlanDatePicker *datePicker = WaterXIB(@"PlanDatePicker");
    self.tableView.tableHeaderView = datePicker;
    
    [datePicker.picker setDate:self.plan.time ? self.plan.time : [NSDate date]];
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc]bk_initWithBarButtonSystemItem:UIBarButtonSystemItemSave handler:^(id sender) {
        self.viewModel.plan.time = [datePicker.picker date];
        self.viewModel.plan.isRemind = YES;
        if (self.save) {
            self.save(self.viewModel.plan);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }];
    self.navigationItem.rightBarButtonItem = editBtn;
    
    
}


#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.viewModel.menus.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.viewModel.menus[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *item = self.viewModel.menus[indexPath.section][indexPath.row];
    cell.textLabel.text = item[@"title"];
    cell.detailTextLabel.text = item[@"value"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *item = self.viewModel.menus[indexPath.section][indexPath.row];
    NSString *title = item[@"title"];
    if ([title isEqualToString:@"标签"]) {
        PlanTagController *tag = [[PlanTagController alloc]init];
        tag.plan = self.viewModel.plan;
        tag.save = ^(PlanItem *plan) {
            self.viewModel.plan = plan;
            [self.viewModel resetMenus];
            [self.tableView reloadData];
        };
        tag.title = @"标签";
        [self.navigationController pushViewController:tag animated:YES];
    }else{
        RingsController *ringsVC = [[RingsController alloc]initWithNibName:@"RingsController" bundle:nil];
        ringsVC.plan = self.viewModel.plan;
        ringsVC.save = ^(PlanItem *plan) {
            self.viewModel.plan = plan;
            [self.viewModel resetMenus];
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:ringsVC animated:YES];
    }
  
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (PlanDetailViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[PlanDetailViewModel alloc]init];
    }
    return _viewModel;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (PlanItem *)plan{
    if (!_plan) {
        _plan = [[PlanItem alloc]init];
    }
    return _plan;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
